/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.internal.configuration;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;

import org.sourceprojects.lycia.internal.logging.LyciaLoggerWrapper;
import org.w3c.dom.Document;

/**
 * 
 * @author noctarius
 * @since 1.9.1
 */
public class NamespaceContextImpl implements NamespaceContext {
	private static final LyciaLoggerWrapper LOGGER = LyciaLoggerWrapper
			.getLogger(NamespaceContextImpl.class);

	private Map<String, String> namespaces = new HashMap<String, String>();
	private Map<String, String> prefixes = new HashMap<String, String>();

	private Document document;

	public void setDocument(final Document document) {
		this.document = document;
	}

	public Document getDocument() {
		return document;
	}

	@Override
	public String getNamespaceURI(final String prefix) {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("Lookup prefix '" + prefix + "' for XPath expression");
		}

		if (prefix == null || "".equals(prefix)) {
			return document.lookupNamespaceURI(XMLConstants.NULL_NS_URI);
		}

		final String namespaceURI = prefixes.get(prefix);

		if (namespaceURI == null && document != null) {
			if (XMLConstants.DEFAULT_NS_PREFIX.equals(prefix)) {
				return document
						.lookupNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX);
			}

			return document.lookupNamespaceURI(prefix);
		}

		return namespaceURI;
	}

	@Override
	public String getPrefix(final String namespaceURI) {
		final String prefix = namespaces.get(namespaceURI);

		if (prefix == null && document != null) {
			return document.lookupPrefix(namespaceURI);
		}

		return prefix;
	}

	@Override
	public Iterator<String> getPrefixes(final String namespaceURI) {
		final String prefix = namespaces.get(namespaceURI);

		final Collection<String> set = Collections
				.unmodifiableCollection(Arrays.asList(prefix));

		return set.iterator();
	}

	public void addNamespaceBinding(final String prefix,
			final String namespaceURI) {
		namespaces.put(namespaceURI, prefix);
		prefixes.put(prefix, namespaceURI);
	}

	@Override
	public String toString() {
		return "NamespaceContextImpl [document=" + document + ", namespaces="
				+ namespaces + ", prefixes=" + prefixes + "]";
	}

}
